<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="中药饮片代码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入中药饮片代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="中药饮片名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入中药饮片名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="药材名称" prop="mName">
        <el-input
          v-model="queryParams.mName"
          placeholder="请输入药材名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="炮制方法" prop="processing">
        <el-input
          v-model="queryParams.processing"
          placeholder="请输入炮制方法"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="药材科" prop="source">
        <el-input
          v-model="queryParams.source"
          placeholder="请输入药材科"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="药材种来源" prop="speciesOrigin">
        <el-input
          v-model="queryParams.speciesOrigin"
          placeholder="请输入药材种来源"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="药用部位" prop="medicinalParts">
        <el-input
          v-model="queryParams.medicinalParts"
          placeholder="请输入药用部位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性味与归经" prop="propertyFlavor">
        <el-input
          v-model="queryParams.propertyFlavor"
          placeholder="请输入性味与归经"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="功能与主治" prop="effect">
        <el-input
          v-model="queryParams.effect"
          placeholder="请输入功能与主治"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用法与用量" prop="usage">
        <el-input
          v-model="queryParams.usage"
          placeholder="请输入用法与用量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:code:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:code:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:code:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:code:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="codeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="中药饮片代码" align="center" prop="code" />
      <el-table-column label="中药饮片名称" align="center" prop="name" />
      <el-table-column label="药材名称" align="center" prop="mName" />
      <el-table-column label="炮制方法" align="center" prop="processing" />
      <el-table-column label="功效分类" align="center" prop="type" />
      <el-table-column label="药材科" align="center" prop="source" />
      <el-table-column label="药材种来源" align="center" prop="speciesOrigin" />
      <el-table-column label="药用部位" align="center" prop="medicinalParts" />
      <el-table-column label="性味与归经" align="center" prop="propertyFlavor" />
      <el-table-column label="功能与主治" align="center" prop="effect" />
      <el-table-column label="用法与用量" align="center" prop="usage" />
      <el-table-column label="医保支付政策" align="center" prop="policy" />
      <el-table-column label="标准名称" align="center" prop="standard" />
      <el-table-column label="页码" align="center" prop="page" />
      <el-table-column label="地区" align="center" prop="area" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:code:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:code:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改中药饮片代码对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="中药饮片代码" prop="code">
          <el-input v-model="form.code" placeholder="请输入中药饮片代码" />
        </el-form-item>
        <el-form-item label="中药饮片名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入中药饮片名称" />
        </el-form-item>
        <el-form-item label="药材名称" prop="mName">
          <el-input v-model="form.mName" placeholder="请输入药材名称" />
        </el-form-item>
        <el-form-item label="炮制方法" prop="processing">
          <el-input v-model="form.processing" placeholder="请输入炮制方法" />
        </el-form-item>
        <el-form-item label="药材科" prop="source">
          <el-input v-model="form.source" placeholder="请输入药材科" />
        </el-form-item>
        <el-form-item label="药材种来源" prop="speciesOrigin">
          <el-input v-model="form.speciesOrigin" placeholder="请输入药材种来源" />
        </el-form-item>
        <el-form-item label="药用部位" prop="medicinalParts">
          <el-input v-model="form.medicinalParts" placeholder="请输入药用部位" />
        </el-form-item>
        <el-form-item label="性味与归经" prop="propertyFlavor">
          <el-input v-model="form.propertyFlavor" placeholder="请输入性味与归经" />
        </el-form-item>
        <el-form-item label="功能与主治" prop="effect">
          <el-input v-model="form.effect" placeholder="请输入功能与主治" />
        </el-form-item>
        <el-form-item label="用法与用量" prop="usage">
          <el-input v-model="form.usage" placeholder="请输入用法与用量" />
        </el-form-item>
        <el-form-item label="医保支付政策" prop="policy">
          <el-input v-model="form.policy" placeholder="请输入医保支付政策" />
        </el-form-item>
        <el-form-item label="标准名称" prop="standard">
          <el-input v-model="form.standard" placeholder="请输入标准名称" />
        </el-form-item>
        <el-form-item label="页码" prop="page">
          <el-input v-model="form.page" placeholder="请输入页码" />
        </el-form-item>
        <el-form-item label="地区" prop="area">
          <el-input v-model="form.area" placeholder="请输入地区" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCode, getCode, delCode, addCode, updateCode } from "@/api/product/code";

export default {
  name: "Code",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 中药饮片代码表格数据
      codeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        mName: null,
        processing: null,
        type: null,
        source: null,
        speciesOrigin: null,
        medicinalParts: null,
        propertyFlavor: null,
        effect: null,
        usage: null,
        policy: null,
        standard: null,
        page: null,
        area: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "中药饮片代码不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "中药饮片名称不能为空", trigger: "blur" }
        ],
        mName: [
          { required: true, message: "药材名称不能为空", trigger: "blur" }
        ],
        processing: [
          { required: true, message: "炮制方法不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "功效分类不能为空", trigger: "change" }
        ],
        source: [
          { required: true, message: "药材科不能为空", trigger: "blur" }
        ],
        speciesOrigin: [
          { required: true, message: "药材种来源不能为空", trigger: "blur" }
        ],
        medicinalParts: [
          { required: true, message: "药用部位不能为空", trigger: "blur" }
        ],
        propertyFlavor: [
          { required: true, message: "性味与归经不能为空", trigger: "blur" }
        ],
        effect: [
          { required: true, message: "功能与主治不能为空", trigger: "blur" }
        ],
        usage: [
          { required: true, message: "用法与用量不能为空", trigger: "blur" }
        ],
        policy: [
          { required: true, message: "医保支付政策不能为空", trigger: "blur" }
        ],
        standard: [
          { required: true, message: "标准名称不能为空", trigger: "blur" }
        ],
        page: [
          { required: true, message: "页码不能为空", trigger: "blur" }
        ],
        area: [
          { required: true, message: "地区不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询中药饮片代码列表 */
    getList() {
      this.loading = true;
      listCode(this.queryParams).then(response => {
        this.codeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        mName: null,
        processing: null,
        type: null,
        source: null,
        speciesOrigin: null,
        medicinalParts: null,
        propertyFlavor: null,
        effect: null,
        usage: null,
        policy: null,
        standard: null,
        page: null,
        area: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加中药饮片代码";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCode(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改中药饮片代码";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCode(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCode(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除中药饮片代码编号为"' + ids + '"的数据项？').then(function() {
        return delCode(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/code/export', {
        ...this.queryParams
      }, `code_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
